package com.hymjweb.demo.hymj.customer_manage.mapper;

import com.hymjweb.demo.framework.util.DataTypeUtil;
import com.hymjweb.demo.hymj.customer_manage.bean.BCustomerPojo;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.jdbc.SQL;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

@Mapper
@Component
public interface BCustomerMapper {

    class BCustomerMapperProvider{

        /**
         * 客户业务员下拉列表,注意使用别名name
         * @return
         */
        public String findSalesman(){
            return new SQL() {{
                SELECT("t.name");
                FROM("xt_user bc");
                WHERE(" t.id in (select b.salesman_id from b_customer b )");
            }}.toString();
        }

        public String findAll(Map map) {

            String sql= new SQL() {{
                SELECT("t.*");
                FROM("b_customer t where 1 = 1 ");
            }}.toString();

            if (map != null) {
                String name = DataTypeUtil.toNotNullString(map.get("keyName"));
                String departmentId = DataTypeUtil.toNotNullString(map.get("keyDepartmentId"));
                String salesmanId = DataTypeUtil.toNotNullString(map.get("keySalesmanId"));

                if(!DataTypeUtil.isEmptyStr(name)){
                    sql += " and (t.name like " + DataTypeUtil.quoteAllLikeStr(name) + " or t.phone like " + DataTypeUtil.quoteAllLikeStr(name) + " )";
                }

                if(!DataTypeUtil.isEmptyStr(departmentId)){
                    sql += " and t.department_id = " + DataTypeUtil.quoteStr(departmentId);
                }

                if(!DataTypeUtil.isEmptyStr(salesmanId)){
                    sql += " and t.salesman_id = " + DataTypeUtil.quoteStr(salesmanId);
                }
            }
            return  sql;
        }


        public String findCacheAll(Map map) {
            String sql = new SQL() {{
                SELECT("t.*");
                FROM("b_customer t");
                if(map != null){
                    String key = DataTypeUtil.toNotNullString(map.get("key"));
                    if(key != null && !"".equals(key)){
                        WHERE("locate('" + key + "',t.name)>0  or locate('" + key.toLowerCase() + "',lower(t.full_pinyin))>0  or locate('" + key.toLowerCase() + "',lower(t.camel_char_pinyin))>0");
                    }
                }
            }}.toString();
            return sql;
        }

        public String insert(BCustomerPojo pojo) {
            String sql = new SQL() {{
                INSERT_INTO("b_customer");
                VALUES("id","#{id}");
                VALUES("name","#{name}");
                VALUES("full_pinyin","#{fullPinyin}");
                VALUES("camel_char_pinyin","#{camelCharPinyin}");
                VALUES("short_name","#{shortName}");
                VALUES("short_name_full_pinyin","#{shortNameFullPinyin}");
                VALUES("short_name_camel_char_pinyin","#{shortNameCamelCharPinyin}");
                VALUES("phone","#{phone}");
                VALUES("address","#{address}");
                VALUES("support_monthly_statement_status_id","#{supportMonthlyStatementStatusId}");
                VALUES("department_id","#{departmentId}");
                VALUES("salesman_id","#{salesmanId}");
                VALUES("credit_term","#{creditTerm}");
                VALUES("arrears_lines","#{arrearsLines}");
                VALUES("air_freight_insurance_rate","#{airFreightInsuranceRate}");
                VALUES("auto_insurance_rate","#{autoInsuranceRate}");
                VALUES("comment","#{comment}");
                VALUES("create_time", "#{createTime}");
                VALUES("create_user_id", "#{createUserId}");
                VALUES("update_time", "#{updateTime}");
                VALUES("update_user_id", "#{updateUserId}");
            }}.toString();
            return sql;
        }

        public String update(BCustomerPojo pojo) {
            String sql= new SQL() {{
                UPDATE("b_customer xd");
                SET("xd.name = #{name}");
                SET("xd.full_pinyin = #{fullPinyin}");
                SET("xd.camel_char_pinyin = #{camelCharPinyin}");
                SET("xd.short_name = #{shortName}");
                SET("xd.short_name_full_pinyin = #{shortNameFullPinyin}");
                SET("xd.short_name_camel_char_pinyin = #{shortNameCamelCharPinyin}");
                SET("xd.phone = #{phone}");
                SET("xd.address = #{address}");
                SET("xd.support_monthly_statement_status_id = #{supportMonthlyStatementStatusId}");
                SET("xd.department_id = #{departmentId}");
                SET("xd.salesman_id = #{salesmanId}");
                SET("xd.credit_term = #{creditTerm}");
                SET("xd.arrears_lines = #{arrearsLines}");
                SET("xd.air_freight_insurance_rate = #{airFreightInsuranceRate}");
                SET("xd.auto_insurance_rate = #{autoInsuranceRate}");
                SET("xd.comment = #{comment}");
                SET("xd.update_time = #{updateTime}");
                SET("xd.update_user_id = #{updateUserId}");
                WHERE("xd.id=#{id}");
            }}.toString();
            return sql;
        }

        public String delete(String id) {
            String sql = new SQL() {{
                DELETE_FROM("b_customer");
                WHERE("id = " + id );
            }}.toString();
            return sql;
        }

        public String queryById(String id) {
            String sql= new SQL() {{
                SELECT("t.*");
                FROM("b_customer t");
                WHERE(" t.id = " + DataTypeUtil.quoteStr(id));
            }}.toString();

            return  sql;
        }


        public String queryDeliveryCustomerByKey(String queryKey){
            String quoteQueryKey = DataTypeUtil.quoteAllLikeStr(queryKey.toLowerCase());
            StringBuffer whereBuffer = new StringBuffer("");
            whereBuffer.append("t.name like ").append(quoteQueryKey);
            whereBuffer.append(" or t.short_name like ").append(quoteQueryKey);
            whereBuffer.append(" or lower(t.full_pinyin) like ").append(quoteQueryKey);
            whereBuffer.append(" or lower(t.camel_char_pinyin) like ").append(quoteQueryKey);
            whereBuffer.append(" or lower(t.short_name_full_pinyin) like ").append(quoteQueryKey);
            whereBuffer.append(" or lower(t.short_name_camel_char_pinyin) like ").append(quoteQueryKey);
            whereBuffer.append(" or t.phone like ").append(quoteQueryKey);

            String sql= new SQL() {{
                SELECT("t.*");
                FROM("b_customer t");
                WHERE(whereBuffer.toString());
            }}.toString();

            return  sql;
        }

        public String findUK(String name,String exceptId) {
            String sql= new SQL() {{
                SELECT("t.*");
                FROM("b_customer t");
                WHERE(" t.name = " + DataTypeUtil.quoteStr(name));
            }}.toString();

            if(!DataTypeUtil.isEmptyStr(exceptId)){
                sql += " and t.id != " + DataTypeUtil.quoteStr(exceptId);
            }

            return  sql;
        }
    }

    @Results(id = "bCustomerFindAllMapId",value = {
            @Result(column = "id", property = "id"),
            @Result(column = "name", property = "name"),
            @Result(column = "full_pinyin", property = "fullPinyin"),
            @Result(column = "camel_char_pinyin", property = "camelCharPinyin"),
            @Result(column = "short_name", property = "shortName"),
            @Result(column = "short_name_full_pinyin", property = "shortNameFullPinyin"),
            @Result(column = "short_name_camel_char_pinyin", property = "shortNameCamelCharPinyin"),
            @Result(column = "phone", property = "phone"),
            @Result(column = "address", property = "address"),
            @Result(column = "support_monthly_statement_status_id", property = "supportMonthlyStatementStatusId"),
            @Result(column = "department_id", property = "departmentId"),
            @Result(column = "salesman_id", property = "salesmanId"),
            @Result(column = "credit_term", property = "creditTerm"),
            @Result(column = "arrears_lines", property = "arrearsLines"),
            @Result(column = "air_freight_insurance_rate", property = "airFreightInsuranceRate"),
            @Result(column = "auto_insurance_rate", property = "autoInsuranceRate"),
            @Result(column = "comment", property = "comment"),
            @Result(column = "create_time", property = "createTime"),
            @Result(column = "create_user_id", property = "createUserId"),
            @Result(column = "update_time", property = "updateTime"),
            @Result(column = "update_user_id", property = "updateUserId"),
    })
    @SelectProvider(type = BCustomerMapperProvider.class, method = "findAll")
    List<BCustomerPojo> findAll(Map map);

    @ResultMap("bCustomerFindAllMapId")
    @SelectProvider(type = BCustomerMapperProvider.class, method = "queryById")
    BCustomerPojo queryById(String id);

    @ResultMap("bCustomerFindAllMapId")
    @SelectProvider(type = BCustomerMapperProvider.class, method = "queryDeliveryCustomerByKey")
    List<BCustomerPojo> queryDeliveryCustomerByKey(String queryKey);

    @ResultMap("bCustomerFindAllMapId")
    @SelectProvider(type = BCustomerMapperProvider.class, method = "findUK")
    BCustomerPojo findUK(String name, String exceptId);

    @ResultMap("bCustomerFindAllMapId")
    @SelectProvider(type = BCustomerMapperProvider.class, method = "findCacheAll")
    List<BCustomerPojo> findCacheAll(Map map);


    @InsertProvider(type = BCustomerMapperProvider.class, method = "insert")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(BCustomerPojo obj);

    @UpdateProvider(type = BCustomerMapperProvider.class, method = "update")
    int update(BCustomerPojo obj);

    @DeleteProvider(type = BCustomerMapperProvider.class, method = "delete")
    int delete(String id);

}
